#include "soundwriter.h"

struct SOUNDWRITER {
    AVFormatContext *oc;
    AVStream *stream;
    AVCodec *codec;
}

/**
 * Write a sample.
 */
void soundwriter_write_sample(SOUNDWRITER *sw, float left, float right) {

}

/**
 * Open sound.
 */
SOUNDWRITER *soundwriter_open(const char *filename, const char *format) {
	SOUNDWRITER *sw=nicemalloc(sizeof(SOUNDWRITER));

    av_register_all();

    avformat_alloc_output_context2(&sw->oc, NULL, format, filename);
    if (!sw->oc)
    	fail("Unable to open audio context.");

    AVOutputFormat *fmt=sw->oc->oformat;
	sw->codec=avcodec_find_encoder(fmt->audio_codec);
    sw->stream=avformat_new_stream(sw->oc,sw->codec);
    if (!sw->stream)
    	fail("Unable to create stream.");

    //sw->stream->id = sw->oc->nb_streams-1;
    AVCodecContext *c=sw->stream->codec;

    sw->stream->id = 1;
    c->sample_fmt  = AV_SAMPLE_FMT_FLTP;
    c->bit_rate    = 64000;
    c->sample_rate = 44100;
    c->channels    = 2;

    if (sw->oc->oformat->flags & AVFMT_GLOBALHEADER)
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
}